/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Info Cram 6000.
 *
 * The Initial Developer of the Original Code is
 * Jiří Mašek.
 * Portions created by the Initial Developer are Copyright (C) 2010
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * ***** END LICENSE BLOCK ***** */

/* ************************************************************************** *
 * ********************************* FILES ********************************** *
 * ************************************************************************** */

/**
 *
 */
infocram.file.blank = function() {
    var _gui = this._parent.gui;
    var _tree = this._parent.tree;

    _gui.removeAll();

    _tree.delay = 1000;
    _tree.def = { };
    _tree.url = "";
    _tree.select = { };

    _gui.editSelect();
}

/**
 *
 */
infocram.file.load = function() {
    var _tree = this._parent.tree;
    var _gui = this._parent.gui;

    var fileChooser = Components.classes["@mozilla.org/filepicker;1"].createInstance(Components.interfaces.nsIFilePicker);

    fileChooser.init(window, "Select a file...", Components.interfaces.nsIFilePicker.modeOpen);

    fileChooser.appendFilters(0x02);
    fileChooser.appendFilters(0x01);

    var fileBox = fileChooser.show();

    if (!fileBox) {
        var file = fileChooser.file;

        // open an input stream from file

        var istream = Components.classes["@mozilla.org/network/file-input-stream;1"].
        createInstance(Components.interfaces.nsIFileInputStream);

        istream.init(file, 0x01, 0444, 0);
        istream.QueryInterface(Components.interfaces.nsILineInputStream);

        // read lines into string

        var line = {}, data = "", hasmore;

        do {
            hasmore = istream.readLine(line);
            data += line.value;
        } while(hasmore);

        istream.close();

        // do something with read data

        var matches = data.match(/extbrain_extract_definition[ ]?=[ ]?\{[\n a-zA-z:'"0-9,\{\}\.#\[\]_\-\/\(\)]*\};/g);

        if (matches) {
            var extbrain_extract_definition;

            eval(matches[0]);

            _tree.url = extbrain_extract_definition.url;
            _tree.delay = extbrain_extract_definition.delay;
            _tree.def = extbrain_extract_definition.def;
            _tree.select = extbrain_extract_definition.select;
            
            matches = data.match(/<title>(.*)<\/title>/g);

            if (matches) {
                _tree.title = matches[1];
            }

            _gui.removeAll();
            _gui.editColumn(0, "url", _tree.url);
            _gui.editing = _gui.SELECT;
            _gui.display(_tree.select);
        }
    }
}

/**
 *
 */
infocram.file.process = function(select, parent, tree) {
    var _gui = this._parent.gui;

    for (var child in select) {
        _gui.addNode(parent, select[child].area, child, select[child].url, select[child].read, select[child].use, select[child].attach);

        if (select[child].select) {
            this.process(select[child].select , tree.view.rowCount - 1, tree);
        }
    }
}

/**
 *
 */
infocram.file.getHtml = function() {
    var _tree = this._parent.tree;

    var html = "<html>\n";

    html += "\t<head>\n";
    html += "\t\t<title>" + _tree.title + "</title>\n";
    html += "\t</head>\n\n";
    html += "\t<body>\n";
    html += "\t\t<script type=\"text/javascript\">\n";
    html += "\t\t\tthis.extbrain_extract_definition = ";

    html += JSON.stringify(_tree.getExtbrainExtractDefinition());

    html += ";\n";
    html += "\t\t</script>\n";
    html += "\t</body>\n";
    html += "</html>";

    return html;
}

/**
 *
 */
infocram.file.save = function() {
    var fileChooser = Components.classes["@mozilla.org/filepicker;1"]
                      .createInstance(Components.interfaces.nsIFilePicker);

    fileChooser.init(window, "Save File", Components.interfaces.nsIFilePicker.modeSave);

    fileChooser.defaultExtension = "html";
    fileChooser.defaultString = "export.html";

    fileChooser.appendFilters(0x02);
    fileChooser.appendFilters(0x01);

    var fileBox = fileChooser.show();

    if (!fileBox) {
        // Saving into the file.

        var file = fileChooser.file;

        var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"].
        createInstance(Components.interfaces.nsIFileOutputStream);

        // use 0x02 | 0x10 to open file for appending.

        foStream.init(file, 0x02 | 0x08 | 0x20, 0666, 0);

        // write, create, truncate

        var converterOutputStream = Components.classes["@mozilla.org/intl/converter-output-stream;1"].

        createInstance(Components.interfaces.nsIConverterOutputStream);

        converterOutputStream.init(foStream, "UTF-8", 0, 0);
        converterOutputStream.writeString(this.getHtml());
        converterOutputStream.close();
    }
}